package com.iap.common.view.control.code
{
	import com.iap.common.model.Favorite;
	import com.iap.common.model.Filler;
	import com.iap.common.model.Sample;
	import com.iap.common.model.SampleFiller;
	import com.iap.common.model.VectorImage;
	import com.iap.common.view.control.FavoriteDisplay;
	import com.iap.common.view.control.VectorImageFillerDisplay;
	
	import flash.events.Event;
	
	import org.osflash.signals.Signal;
	
	import spark.components.Group;
	
	public class FavoriteDisplayCode extends Group
	{
		private var displayWidth:uint = 200;
		protected var view:FavoriteDisplay;
		
		protected var model:Favorite;
		protected var sample:Sample;
		protected var vectorModels:Array;
		protected var sampleFillers:Array;
		private var loadedImages:int = 0;

		public function FavoriteDisplayCode()
		{
			super();
		}

		public function setSource(model:Favorite, displayWidth:uint = 0):void
		{
			this.model = model;
			this.sample = model.sample;
			if(sample==null) return;
			this.vectorModels = sample.vectorModel.vectorImages;
			this.sampleFillers = sample.sampleFillers;
			this.displayWidth = displayWidth>0 ? displayWidth : this.displayWidth;
			addImages();
		}
		
		protected function init(view:FavoriteDisplay):void
		{
			this.view = view;
			addImages();
		}
		
		private function addImages():void
		{
			if(view == null) return;
			if(model == null) return;
			clearImages();
			for each(var item:VectorImage in vectorModels)
			{
				addImage(item, getSampleFillerByLayer(item.layer));
			}
		}
		
		private function getSampleFillerByLayer(layer:uint):SampleFiller
		{
			var result:SampleFiller = null;
			for each(var item:SampleFiller in sampleFillers)
			{
				if(item.layer == layer)
					result = item;
			}
			return result;
		}
		
		private function clearImages():void
		{
			view.main.removeAllElements();
		}
		
		private function addImage(item:VectorImage, sampleFiller:SampleFiller = null):void
		{
			loadedImages++;
			var control:VectorImageFillerDisplay = new VectorImageFillerDisplay();
			control.width = displayWidth;
			control.height = displayWidth;
			var filler:Filler = sampleFiller ? sampleFiller.filler : null;
			control.setSource(item, filler);
			view.main.addElement(control);
			control.addEventListener(VectorImageFillerDisplay.EVENT_READY, vectorImageReadyHandler);
		}
		
		private function vectorImageReadyHandler(evt:Event):void
		{
			var control:VectorImageFillerDisplay = evt.target as VectorImageFillerDisplay;
			control.removeEventListener(VectorImageFillerDisplay.EVENT_READY, vectorImageReadyHandler);
			loadedImages--;
			if(loadedImages<=0)
			{
				dispatchEvent(new Event(FavoriteDisplay.EVENT_READY));
			}
		}
		
		public function setSampleFiller(sampleFiller:SampleFiller):void
		{
			var filler:Filler = sampleFiller.filler;
			var layer:uint = sampleFiller.layer;
			var idx:uint = layer>0 ? layer-1 : 0; 
			if(view.main.getElementAt(idx) == null) return;
			
			var control:VectorImageFillerDisplay = view.main.getElementAt(idx) as VectorImageFillerDisplay;
			control.setFiller(filler);
		}
	}
}